#ifndef __TRIANGLE_PAIR_TEST_SIMULATOR__
#define __TRIANGLE_PAIR_TEST_SIMULATOR__

#include <llib.h>

#include <vector>
#include <set>
#include <boost/timer.hpp>

#include "collisionBase.h"

using namespace llib;
using namespace llib::geom;

#define	TRIANGLE_NUM	2

class TrianglePairSimulator : public Simulator {
public:
	TrianglePairSimulator():
	  _validColor		( 0.0, 1.0, 0.0 ),
	  _invalidColor		( 1.0, 0.0, 0.0 ),
	  _timeStepId		( -1 ),
	  _deltaT			( 0.01f ),
	  _locationMaxBound	( 10.f ),
	  _locationMinBound ( -10.f ),
	  _velocityMaxBound ( 5.0f ),
	  _velocityMinBound ( -5.0f )	{}

	Boolean	Initialize();
	Boolean Render();

	Boolean OnKeyDown(Uint8 key, Int32 x, Int32 y, Uint8 modifiers);

private:
	Triangle3f		_startLoc[TRIANGLE_NUM];
	Triangle3f		_curLoc[TRIANGLE_NUM];
	Triangle3f		_endLoc[TRIANGLE_NUM];
	Triangle3f		_velocity[TRIANGLE_NUM];

	Triangle3b		_boundCollision[TRIANGLE_NUM];
	Boolean			_pairCollision[TRIANGLE_NUM];

	std::set<CollisionInstant>
					_collisionInstants;
	std::set<CollisionInstant>::iterator
					_ciSetIter;

	boost::timer	_timer;
	Uint32			_timeStepId;
	const Float		_deltaT;

	const Float		_locationMaxBound,	_locationMinBound,
					_velocityMaxBound,	_velocityMinBound;

	const
	Color			_validColor,
					_invalidColor;

private:
	void	reInitScene();
		void	resetLocation();
		void	resetVelocity();
			Float	randomize( Float maxBound, Float minBound );

	void	framePreProcess();
		// update end locations at the end of frame with a bound collision check
		void	endLocationUpdate();
		// check for collisions between triangle pairs
		void	triangleCollisionCheck();

	void	framePostProcess();
	Boolean	checkNewCollisionInstance(Float timeInStep);

	// check whether current point is between the line segment of start
	Boolean boundInsideCheck(Vector3f &start, Vector3f &end, Vector3f &current);
};

#endif